// pages/pcenter/index.js
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    popShow: false,
    appUserInfo: {
      userName: '',
    },
    isLogin: false,
    index: 0
  },
  onClickHide() {
    this.setData({
      popShow: false
    });
  },
  // 立即邀请
  invite() {
    console.log("立即邀请")
  },
  goLogin() {
    if (!this.data.isLogin) {
      wx.navigateTo({
        url: '../login/index',
      });
    }
  },
  //退出登录
  goSetting() {
    wx.navigateTo({
      url: '../setting/index',
    });
  },
  //切换客户账号
  companyUserChange: function (e) {
    var index = e.detail.value;
    var that = this;
    let userInfo = wx.getStorageSync('userInfoList')[index];
    wx.request({
      url: api.changeLoginUser,
      data: {
        id: userInfo.id
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.result == 1) {
          that.setData({
            index: index,
            isLogin: true,
            appUserInfo: res.data.info.appUserInfo,
            userInfoList: res.data.info.userInfoList
          });
          wx.removeStorageSync('token');
          wx.removeStorageSync('isLogin');
          wx.removeStorageSync('appUserInfo');
          wx.removeStorageSync('index');
          wx.setStorageSync('isLogin', true);
          wx.setStorageSync('index', index);
          wx.setStorageSync('appUserInfo', res.data.info.appUserInfo);
          wx.setStorageSync('token', res.data.info.accessToken);
          wx.setStorageSync('companyId', res.data.info.accessToken);
        } else {
          util.showErrorToast(res.data.msg);
        }
      }
    });
  },
  goTask(){
    wx.navigateTo({
      url: '/pages/task-plan/index',
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync('isLogin') == true) {
      this.setData({
        isLogin: true,
        appUserInfo: wx.getStorageSync('appUserInfo'),
        userInfoList: wx.getStorageSync('userInfoList'),
        index: wx.getStorageSync('index')
      });
    } else {
      this.setData({
        isLogin: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})